home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 53.5 KB | 1,866 lines | [TEXT/MPS ] |
- //----------------------------------------------------------------------------------------
- // UMacAppUtilities.cp
- // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
- //----------------------------------------------------------------------------------------
-
- #ifndef __UMACAPPUTILITIES__
- #include "UMacAppUtilities.h"
- #endif
-
- // MacApp
-
- #ifndef __MACAPPVERSION__
- #include "MacAppVersion.h"
- #endif
-
- #ifndef __UDEBUG__
- #include "UDebug.h"
- #endif
-
- #ifndef __UDISPATCHER__
- #include "UDispatcher.h"
- #endif
-
- #ifndef __UGEOMETRY__
- #include "UGeometry.h"
- #endif
-
- #ifndef __UMACAPPGLOBALS__
- #include "UMacAppGlobals.h"
- #endif
-
- #ifndef __UMEMORY__
- #include "UMemory.h"
- #endif
-
- #ifndef __UNIVERSALSTARTUP__
- #include "UniversalStartup.h"
- #endif
-
- #ifndef __USTREAM__
- #include "UStream.h"
- #endif
-
- #ifndef __UWINDOW__
- #include "UWindow.h"
- #endif
-
- // OpenDoc
-
- #if qOpenDoc
- #ifndef SOM_ODPart_xh
- #include "Part.xh"
- #endif
-
- #endif
-
- // Toolbox
-
- #ifndef __DEVICES__
- #include <Devices.h>
- #endif
-
- #ifndef __DIALOGS__
- #include <Dialogs.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __FONTS__
- #include <Fonts.h>
- #endif
-
- #if qPowerPC || qModelCFM
- #ifndef __FRAGLOAD__
- #include <FragLoad.h>
- #endif
-
- #endif
-
- #ifndef __GESTALT__
- #include <Gestalt.h>
- #endif
-
- #ifndef __LOWMEM__
- #include <LowMem.h>
- #endif
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
-
- #ifndef __OSEVENTS__
- #include <OSEvents.h>
- #endif
-
- #ifndef __PACKAGES__
- #include <Packages.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __SCRAP__
- #include <Scrap.h>
- #endif
-
- #ifndef __SCRIPT__
- #include <Script.h>
- #endif
-
- #ifndef __TEXTUTILS__
- #include <TextUtils.h>
- #endif
-
- #if qNeedsThreads
- #ifndef __THREADS__
- #include <Threads.h>
- #endif
-
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #ifndef __TRAPS__
- #include <Traps.h>
- #endif
-
- // ANSI
-
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
-
- //----------------------------------------------------------------------------------------
- // extern definitions:
- extern WindowRef gWorkPort;
-
- // constants
-
- #if qOldConfigurationFlags
- Configuration gConfiguration;
- #endif
-
- #if qDebugMsg
- const CStr15 gBoolString[2] = { "false", "true"};
- #endif
-
- Boolean gUDialogInitialized;
- Boolean gUGridViewInitialized;
- Boolean gUPrintingInitialized;
- Boolean gUTEViewInitialized;
-
- TEHandle gMATextBoxTE;
- WordBreakUPP gTEDefaultWordBreak;
- const VHSelect gOrthogonal[2] = { hSel, vSel};
-
- const CRect gZeroRect(0, 0, 0, 0);
- const CPoint gZeroPt(0, 0);
- const VPoint gZeroVPt(0, 0);
- const VRect gZeroVRect(0, 0, 0, 0);
-
- const CPoint kBestSystemLocation(-1, -1);
-
- const CStr2 gEmptyString(""); // Constructor keeps this from getting dead-stripped
-
- // the primary colors
- const CRGBColor gRGBBlack(0, 0, 0);
- const CRGBColor gRGBWhite(0xFFFF, 0xFFFF, 0xFFFF);
- const CRGBColor gRGBRed(0xFFFF, 0, 0);
- const CRGBColor gRGBGreen(0, 0xFFFF, 0);
- const CRGBColor gRGBBlue(0, 0, 0xFFFF);
-
-
- //========================================================================================
- // GLOBAL Procedures
- //========================================================================================
- static pascal void StdNoRect(GrafVerb /* verb */,
- const CRect& /* r */ );
- static void InitMyPrivateTE(const CRect& box,
- short heapSize);
- static void CheckAdornment(CntlAdornment p,
- CntlAdornment alias,
- CStr255 theString,
- char* name);
- static void CheckStyleItem(unsigned char s,
- unsigned char alias,
- CStr255 theString,
- char* name);
-
- //========================================================================================
- // CLASS CWhileOutlinePreferred
- //========================================================================================
- #undef Inherited
-
- //----------------------------------------------------------------------------------------
- // CWhileOutlinePreferred::CWhileOutlinePreferred:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- CWhileOutlinePreferred::CWhileOutlinePreferred(Boolean preferOutline)
- {
- // initialize data members
- fPreferOutline = preferOutline;
- fOutlinePreferredChanged = FALSE;
-
- // change outline preferred, if necessary
- if (HasTrueType())
- {
- fOutlinePreferredChanged = (GetOutlinePreferred() != fPreferOutline);
- if (fOutlinePreferredChanged)
- SetOutlinePreferred(fPreferOutline);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CWhileOutlinePreferred::~CWhileOutlinePreferred:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- CWhileOutlinePreferred::~CWhileOutlinePreferred()
- {
- // restore outline preferred, if necessary
- if (HasTrueType() && fOutlinePreferredChanged)
- SetOutlinePreferred(!fPreferOutline);
- }
-
-
- //========================================================================================
- // CLASS CStringListRsrc
- //========================================================================================
- #undef Inherited
-
- //----------------------------------------------------------------------------------------
- // CStringListRsrc::GetString:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void CStringListRsrc::GetString(short index,
- CStr255& theString) const
- {
- if (fStrListID != kNoResource)
- GetIndString(theString, fStrListID, index);
- }
-
- //----------------------------------------------------------------------------------------
- // CStringListRsrc::FindString:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- short CStringListRsrc::FindString(const CStr255& theString,
- Boolean addString)
- {
- short numStrings = CountStrings();
-
- for (short i = 1; i <= numStrings; ++i)
- {
- CStr255 testString;
- GetString(i, testString);
-
- if (testString == theString)
- return i; // found it! return the index
- }
-
- // CString is not found, should we add it?
- if (addString == kAddString)
- return AppendString(theString); // added it! return the index
-
- // CString not found...sigh!
- return kNoResource;
- }
-
- //----------------------------------------------------------------------------------------
- // CStringListRsrc::AppendString:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- short CStringListRsrc::AppendString(const CStr255& theString)
- {
- short returnVal = kNoResource;
-
- short totalStringLength = theString.Length() + kLengthByte;
- Handle aHandle = GetResource('STR#', fStrListID);
- if (aHandle)
- {
- Size itsSize = GetHandleSize(aHandle);
- if (itsSize > 0)
- {
- short numStrings = CountStrings();
- SetPermHandleSize(aHandle, itsSize + totalStringLength);
- (**((IntegerHandle)aHandle)) =++numStrings;// increment the count of strings in the STR# resource
-
- // needs a failure handler
- long result = Munger(aHandle, itsSize, NULL, totalStringLength, &theString, totalStringLength);
- if (result > 0)
- {
- // mark it as changed
- ChangedResource(aHandle);
-
- returnVal = numStrings;
- }
- }
- }
- else
- {
- // must create the resource...
- aHandle = NewPermHandle(sizeof(short) + totalStringLength);
- (**((IntegerHandle)aHandle)) = 1; // set the count of strings in the STR# resource
- long result = Munger(aHandle, sizeof(short), NULL, totalStringLength, &theString, totalStringLength);
- if (result > 0)
- {
- AddResource(aHandle, 'STR#', fStrListID, fStrListRsrcName);
- FailResError(); // if fails, it's most likely because the file is read-only
-
- SetResAttrs(aHandle, resPurgeable);
-
- // Need the following ChangedResource call because the SetResAttrs call on the previous
- // line cleared the "resChanged" attribute of the resource.
- ChangedResource(aHandle);
-
- returnVal = 1;
- }
- }
-
- return returnVal;
- }
-
- //----------------------------------------------------------------------------------------
- // CStringListRsrc::ClearAll:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void CStringListRsrc::ClearAll()
- {
- Handle aHandle = GetResource('STR#', fStrListID);
- if (aHandle)
- {
- SetPermHandleSize(aHandle, sizeof(short));
- (**((IntegerHandle)aHandle)) = 0; // set the count of strings in the STR# resource
-
- // mark it as changed
- ChangedResource(aHandle);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CStringListRsrc::CountStrings:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- short CStringListRsrc::CountStrings() const
- {
- // return the leading integer in the STR# resource:
- //
- // type 'STR#' {
- // integer = $$Countof(StringArray);
- // array StringArray {
- // pstring;
- // };
- // };
-
- Handle aHandle = GetResource('STR#', fStrListID);
-
- return aHandle ? **((IntegerHandle)aHandle) : kNoResource;
- }
-
- //----------------------------------------------------------------------------------------
- // CStringListRsrc::GetListName:
- //----------------------------------------------------------------------------------------
- #pragma segment MARes
-
- void CStringListRsrc::GetListName(CStr255& theString)
- {
- // Do we have the name in our field?
- if (fStrListRsrcName.Length() > 0)
- theString = fStrListRsrcName;
- else // We don't have the name so try and get it from the resource
- {
- // Get the resource's handle
- Handle aHandle = GetResource('STR#', fStrListID);
- if (aHandle)
- {
- short itsRsrcID;
- CStr255 itsName;
- ResType itsType;
-
- // Extract the resource info from the handle
- GetResInfo(aHandle, &itsRsrcID, &itsType, itsName);
- if (itsName.Length() > 0)
- {
- theString = itsName;
-
- // Store the name in our field
- fStrListRsrcName = theString;
- }
-
- ReleaseResource(aHandle);
- }
- else // Sorry we don't have anything so return an empty CString
- theString.Empty();
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CStringListRsrc::RemoveAt:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void CStringListRsrc::RemoveAt(short index)
- {
- if ((index > 0) && (index <= CountStrings()))
- {
- Handle aHandle = GetResource('STR#', fStrListID);
- if (aHandle)
- {
- Size originalSize = GetHandleSize(aHandle);
-
- // determine the accumulated size
- Size accumulatedSize = sizeof(short);// the accumulated size is the size of the count
- for (short i = 1; i < index; ++i)
- accumulatedSize += (*((CStringPtr)(*aHandle + accumulatedSize))).Length() + kLengthByte;
-
- // determine the actual CString size
- Size stringSize = (*((CStringPtr)(*aHandle + accumulatedSize))).Length() + kLengthByte;
-
- // move the the data in the handle
- MABlockMove(*aHandle + accumulatedSize + stringSize, *aHandle + accumulatedSize, originalSize - accumulatedSize - stringSize);
-
- // trim the handle down to its new size
- SetPermHandleSize(aHandle, originalSize - stringSize);
-
- // decrement count of strings in STR# resource
- --(**((IntegerHandle)aHandle));
-
- // mark it as changed
- ChangedResource(aHandle);
- }
- #if qDebugMsg
- else
- fprintf(stderr, "CStringListRsrc::RemoveAt can't find STR# = %d.\n", fStrListID);
- #endif
-
- }
- #if qDebugMsg
- else
- fprintf(stderr, "CStringListRsrc::RemoveAt there's no CString at %d to remove!\n", index);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // CStringListRsrc::ReplaceAt:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void CStringListRsrc::ReplaceAt(const CStr255& theString,
- short index)
- {
- if ((index > 0) && (index <= CountStrings()))
- {
- Handle aHandle = GetResource('STR#', fStrListID);
- if (aHandle)
- {
- Size newStringSize = theString.Length() + kLengthByte;// the actual CString size
- Size originalSize = GetHandleSize(aHandle);
-
- // determine the accumulated size of the valid portion of the resource
- Size accumulatedSize = sizeof(short);// the accumulated size is the size of the count
- for (short i = 1; i < index; ++i)
- accumulatedSize += (*((CStringPtr)(*aHandle + accumulatedSize))).Length() + kLengthByte;
-
- // determine the actual CString size
- Size oldStringSize = (*((CStringPtr)(*aHandle + accumulatedSize))).Length() + kLengthByte;
-
- // determine the new handle size
- Size newHandleSize = originalSize + (newStringSize - oldStringSize);
-
- // determine whether to grow or shrink the handle?
- if (newStringSize > oldStringSize)
- {
- // grow the handle
- SetPermHandleSize(aHandle, newHandleSize);
-
- // move the bytes
- MABlockMove(*aHandle + accumulatedSize + oldStringSize, *aHandle + accumulatedSize + newStringSize, originalSize - accumulatedSize - oldStringSize);
- }
- else if (oldStringSize > newStringSize)
- {
- // move the bytes
- MABlockMove(*aHandle + accumulatedSize + oldStringSize, *aHandle + accumulatedSize + newStringSize, originalSize - accumulatedSize - oldStringSize);
-
- // shrink the handle
- SetPermHandleSize(aHandle, newHandleSize);
- }
-
- // assign the CString
- *((CStr255 *)(*aHandle + accumulatedSize)) = theString;
-
- // mark it as changed
- ChangedResource(aHandle);
- }
- #if qDebugMsg
- else
- fprintf(stderr, "CStringListRsrc::ReplaceAt can't find STR# = %d.\n", fStrListID);
- #endif
-
- }
- #if qDebugMsg
- else
- fprintf(stderr, "CStringListRsrc::ReplaceAt there's no CString at %d to replace!\n", index);
- #endif
- }
-
-
- //========================================================================================
- // CLASS CWMgrIterator
- //========================================================================================
- #undef Inherited
- #define Inherited CIterator
-
- //----------------------------------------------------------------------------------------
- // CWMgrIterator::CWMgrIterator:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- CWMgrIterator::CWMgrIterator(Boolean itsForward) :
- fIterateForward(itsForward)
- {
- Reset();
- }
-
- //----------------------------------------------------------------------------------------
- // CWMgrIterator::Reset:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void CWMgrIterator::Reset()
- {
- fCurrentWindow = fIterateForward ? FirstWindow() : LastWindow();
- }
-
- //----------------------------------------------------------------------------------------
- // CWMgrIterator::MAUtilitiesRes:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- WindowRef CWMgrIterator::FirstWMgrWindow()
- {
- Reset();
-
- return fCurrentWindow; // Always return the first window
- }
-
- //----------------------------------------------------------------------------------------
- // CWMgrIterator::NextWMgrWindow:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- WindowRef CWMgrIterator::NextWMgrWindow()
- {
- Advance();
-
- return fCurrentWindow;
- }
-
- //----------------------------------------------------------------------------------------
- // CWMgrIterator::Advance:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void CWMgrIterator::Advance()
- {
- if (fCurrentWindow)
- fCurrentWindow = fIterateForward ? NextWindow(fCurrentWindow) : PreviousWindow(fCurrentWindow);
- }
-
- //----------------------------------------------------------------------------------------
- // CWMgrIterator::FirstWindow:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
- WindowRef CWMgrIterator::FirstWindow()
- {
- WindowRef currentWindow = (WindowRef)LMGetWindowList();
- if (currentWindow && (currentWindow == gWorkPort))
- currentWindow = NextWindow(currentWindow);
-
- return currentWindow;
- }
-
- //----------------------------------------------------------------------------------------
- // CWMgrIterator::NextWindow:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
- WindowRef CWMgrIterator::NextWindow(WindowRef aWindow)
- {
- WindowRef currentWindow = GetNextWindow(aWindow);
-
- while (currentWindow && (currentWindow == gWorkPort))
- // ignore the work window
- currentWindow = GetNextWindow(currentWindow);
-
- return currentWindow;
- }
-
- //----------------------------------------------------------------------------------------
- // CWMgrIterator::PreviousWindow:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
- WindowRef CWMgrIterator::PreviousWindow(WindowRef aWindow)
- {
- WindowRef currentWindow = FirstWindow();
- while (currentWindow && (NextWindow(currentWindow) != aWindow))
- currentWindow = NextWindow(currentWindow);
-
- return currentWindow;
- }
-
-
- //----------------------------------------------------------------------------------------
- // CWMgrIterator::LastWindow:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
- WindowRef CWMgrIterator::LastWindow()
- {
- WindowRef currentWindow = FirstWindow();
- while (currentWindow && NextWindow(currentWindow))
- currentWindow = NextWindow(currentWindow);
-
- return currentWindow;
- }
-
-
- //========================================================================================
- // GLOBAL Procedures
- //========================================================================================
- #undef Inherited
-
- //----------------------------------------------------------------------------------------
- // CanWriteLn:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- #if qDebug
- Boolean CanWriteLn()
- {
- return DebugCanWriteLn();
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // CanReadLn:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- #if qDebug
- Boolean CanReadLn()
- {
- return DebugCanReadLn();
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // CenterRectOnScreen:
- //----------------------------------------------------------------------------------------
- // This must always be in a resident segment as aRect may be within a handle
- #pragma segment MAUtilitiesRes
-
- void CenterRectOnScreen(CRect& aRect,
- Boolean horizontally,
- Boolean vertically,
- Boolean forDialog)
- {
- short newSize = 0;
- CPoint rectSize(aRect.GetSize());
-
- CPoint screenSize(((CRect &)qd.screenBits.bounds).GetSize());
- screenSize.v -= GetMBarHeight();
-
- // Calculate screen size minus menu bar
- if (horizontally)
- aRect.left = (screenSize.h - rectSize.h) / 2;
- if (vertically)
- if (forDialog)
- {
- newSize = (screenSize.v - rectSize.v) / 5;
- aRect.top = (short)(Max(newSize, 10) + GetMBarHeight());
- }
- else
- aRect.top = (screenSize.v - rectSize.v) / 2;
-
- aRect[botRight] = aRect[topLeft] + rectSize;
- }
-
- //----------------------------------------------------------------------------------------
- // ConcatNumber:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void ConcatNumber(const CStr255& aString,
- long aNumber,
- CStr255& theResult)
- {
- NumToString(aNumber, theResult);
- theResult = aString + theResult;
- }
-
- //----------------------------------------------------------------------------------------
- // PullApplicationToFront:
- //----------------------------------------------------------------------------------------
- // Must be in main segment as it is called in early initialization && in MacAppAlert
- #pragma segment Main
-
- void PullApplicationToFront()
- {
- EventRecord theEvent;
-
- // The "Programmer's guide to MultiFinder says make an event call several times. I
- // guess 3 calls counts as several. Also, it says call GetNextEvent but we don't want
- // to lose events on the floor so we use EventAvail since it seems to work OK
- for (short i = 1; i <= 3; ++i)
- EventAvail(everyEvent, &theEvent);
- }
-
- //----------------------------------------------------------------------------------------
- // MAInteractWithUser:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- OSErr MAInteractWithUser(long timeOutInTicks,
- NMRecPtr nmReqPtr,
- AEIdleUPP idleProc)
- {
- return gDispatcher->InteractWithUser(timeOutInTicks, nmReqPtr, idleProc);
- }
-
- //----------------------------------------------------------------------------------------
- // MAInteractWithUser:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- OSErr MAInteractWithUser()
- {
- return gDispatcher->InteractWithUser(kMacAppTimeout, gNotificationPtr, TAppleEvent::fgIdleProc);
- }
-
- //----------------------------------------------------------------------------------------
- // MAInteractWithUserNoIdleProc:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- OSErr MAInteractWithUserNoIdleProc()
- {
- return gDispatcher->InteractWithUser(kMacAppTimeout, gNotificationPtr, NULL);
- }
-
- #if !qNeedsSystem7_5
- //----------------------------------------------------------------------------------------
- // MACharacterByteType:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
- short MACharacterByteType(Ptr textBuf,
- short textOffset,
- ScriptCode script)
- {
- // CharacterByteType only avail in sys 7.1+
- short byteType;
- if (GetSystemVersion() >= 0x0710)
- byteType = CharacterByteType(textBuf, textOffset, script);// textOffset is zero-based
- else
- {
- short oldScript = SetKeyScript(script);
- byteType = CharByte(textBuf, textOffset);// textOffset is zero-based
- SetKeyScript(oldScript);
- }
-
- #if qDebug
- Assertion(byteType != smMiddleByte, "\pbyteType != smMiddleByte. We should never receive smMiddleByte");
- #endif
-
- return byteType;
- }
-
- //----------------------------------------------------------------------------------------
- // MACharacterByte:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
- short MACharacterType(Ptr textBuf,
- short textOffset,
- ScriptCode script)
- {
- // CharacterType only avail in sys 7.1+
- short byteType;
- if (GetSystemVersion() >= 0x0710)
- byteType = CharacterType(textBuf, textOffset, script);// textOffset is zero-based
- else
- {
- short oldScript = SetKeyScript(script);
- byteType = CharType(textBuf, textOffset);// textOffset is zero-based
- SetKeyScript(oldScript);
- }
-
- #if qDebug
- Assertion(byteType != smMiddleByte, "\pbyteType != smMiddleByte. We should never receive smMiddleByte");
- #endif
-
- return byteType;
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // CompareMultiByteChars:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- Boolean CompareMultiByteChars(const CStr31& first,
- const CStr31& second,
- Boolean caseSens)
- {
- Boolean areEqual = FALSE;
-
- if (caseSens)
- areEqual = first == second;
- else
- {
- short textOffset = 0;
- Boolean done = FALSE;
-
- do
- {
- ++textOffset;
- char c = first[textOffset];
-
- short byteType = MACharacterByteType((Ptr) & c, 0, smCurrentScript);
-
- areEqual = first[textOffset] == second[textOffset];
- switch (byteType)
- {
- // special case single byte characters to allow lower case characters to map to
- // upper case characters
- case smSingleByte:
- areEqual = (LowerChar(first[1]) == LowerChar(second[1]));
- done = TRUE;
- break;
-
- case smFirstByte:
- done = !areEqual; // we're done if they don't match
- break;
-
- case smLastByte:
- done = TRUE;
- break;
- }
- } while (!done);
- }
-
- return areEqual;
- }
-
- //----------------------------------------------------------------------------------------
- // DefaultSize:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void DefaultSize(short& theSize)
- {
- if (theSize == GetDefFontSize())
- theSize = 0;
- }
-
- //----------------------------------------------------------------------------------------
- // DisposeIfPicHandle:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- PicHandle DisposeIfPicHandle(PicHandle aPicHandle)
- {
- if (aPicHandle)
- {
- #if !qDebug
- KillPicture(aPicHandle);
- #else
- char msg[128];
-
- if (IsHandle((Handle)aPicHandle)) // Test handlehood
- {
- // Set the handle contents to a real nice value for any dangling pointerciples
- BlockSet(*((Handle)aPicHandle), GetHandleSize((Handle)aPicHandle), kDisposedInitVal);
- KillPicture(aPicHandle);
- }
- else
- {
- VerboseIsHandle((Handle)aPicHandle);// Get the diagnosis printed
- sprintf(msg, "Trying to dispose an invalid PicHandle)\nBad PicHandle: %p\n", aPicHandle);
- ProgramBreak(msg);
- }
- #endif
-
- }
-
- return NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // DisposeIfRgnHandle:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- RgnHandle DisposeIfRgnHandle(RgnHandle aRgnHandle)
- {
- if (aRgnHandle)
- {
- #if !qDebug
- DisposeRgn(aRgnHandle);
- #else
- char msg[128];
-
- // Test handlehood
- if (IsHandle((Handle)aRgnHandle))
- {
- // Set the handle contents to a real nice value for any dangling pointerciples
- BlockSet(*((Handle)aRgnHandle), GetHandleSize((Handle)aRgnHandle), kDisposedInitVal);
- DisposeRgn(aRgnHandle);
- }
- else
- {
- VerboseIsHandle((Handle)aRgnHandle);// Get the diagnosis printed
- sprintf(msg, "Trying to dispose an invalid RgnHandle.\nBad RgnHandle: %p\n", aRgnHandle);
- ProgramBreak(msg);
- }
- #endif
-
- }
-
- return NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // DisposeIfSectionHandle:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- SectionHandle DisposeIfSectionHandle(SectionHandle aSectionHandle)
- {
- if (aSectionHandle)
- {
- #if !qDebug
- DisposeIfHandle((Handle)(*aSectionHandle)->alias);// dispose of the alias
- DisposeIfHandle((Handle)aSectionHandle);// dispose of the SectionHandle
- #else
- char msg[128];
-
- // Test handlehood
- if (IsHandle((Handle)aSectionHandle))
- {
- DisposeIfHandle((Handle)(*aSectionHandle)->alias);// dispose of the alias
-
- // Set the handle contents to a real nice value for any dangling pointerciples
- BlockSet(*((Handle)aSectionHandle), GetHandleSize((Handle)aSectionHandle), kDisposedInitVal);
- DisposeIfHandle((Handle)aSectionHandle);// dispose of the SectionHandle
- }
- else
- {
- VerboseIsHandle((Handle)aSectionHandle);// Get the diagnosis printed
- sprintf(msg, "Trying to dispose an invalid SectionHandle.\nBad SectionHandle: %p\n", aSectionHandle);
- ProgramBreak(msg);
- }
- #endif
-
- }
-
- return NULL;
- }
-
-
- //----------------------------------------------------------------------------------------
- // FindWindowBefore: returns the window just before a given window. Returns NULL if the
- // given window is frontmost or not found.
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- WindowRef FindWindowBefore(WindowRef theWindow)
- {
- WindowRef returnVal = NULL;
-
- CWMgrIterator iter;
-
- for (WindowRef theWMgrWindow = iter.FirstWMgrWindow(); iter.More(); theWMgrWindow = iter.NextWMgrWindow())
- {
- if (GetNextWindow(theWMgrWindow) == theWindow)
- {
- returnVal = theWMgrWindow;
- break;
- }
- }
-
- return returnVal;
- }
-
- //----------------------------------------------------------------------------------------
- // GetActualJustification:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- short GetActualJustification(short justification)
- {
- return justification == teFlushDefault ? GetSysDirection() : justification;
- }
-
- //----------------------------------------------------------------------------------------
- // GetDeskTopRegion:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void GetDeskTopRegion(RgnHandle deskTopRgn)
- {
- if (qNeedsColorQD || HasColorQD()) // deskTopRgn = main screen CRect
- RectRgn(deskTopRgn, &((*GetMainDevice())->gdRect));
- else
- RectRgn(deskTopRgn, &(qd.screenBits.bounds));
- UnionRgn(GetGrayRgn(), deskTopRgn, deskTopRgn);// deskTopRgn = grayRgn + deskTopRgn
- }
-
- //----------------------------------------------------------------------------------------
- // ShowGlobalRegion:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void ShowGlobalRegion(RgnHandle globalRegion)
- {
- if (globalRegion)
- {
- GrafPtr savedPort;
- CGrafPort desktopPort;
- long ticks;
-
- // Cursor region is in global coords. Need to create desktop port
- GetPort(&savedPort);
- if (qNeedsColorQD || HasColorQD())
- OpenCPort(&desktopPort);
- else
- OpenPort((GrafPtr) & desktopPort);
-
- CopyRgn(GetGrayRgn(), TWindow::GetVisRegion((GrafPtr) & desktopPort));
- desktopPort.portRect = (*TWindow::GetVisRegion((GrafPtr) & desktopPort))->rgnBBox;
-
- PenNormal();
- PenMode(patXor);
-
- PaintRgn(globalRegion);
- Delay(30, &ticks);
- PaintRgn(globalRegion);
-
- if (qNeedsColorQD || HasColorQD())
- CloseCPort(&desktopPort);
- else
- ClosePort((GrafPtr) & desktopPort);
- SetPort(savedPort);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // ShowViewRegion:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void ShowViewRegion(TView* theView,
- RgnHandle viewRegion)
- {
- if (viewRegion && theView)
- {
- CTemporaryRegion tempRegion;
-
- CopyRgn(viewRegion, tempRegion);
- theView->LocalToGlobalRegion(tempRegion);
- ShowGlobalRegion(tempRegion);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // GetFontNum:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- short GetFontNum(const CStr255& fontName)
- {
- short fontNum;
- CStr255 localFontName(fontName);
-
- UprStr255(localFontName);
- if (localFontName == CStr255(kSysFontName))
- fontNum = GetSysFont();
- else if (localFontName == CStr255(kApplFontName))
- fontNum = GetAppFont();
- else
- GetFNum(fontName, &fontNum);
-
- return fontNum;
- }
-
- //----------------------------------------------------------------------------------------
- // GetIfBkColor:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- CRGBColor GetIfBkColor()
- {
- // Since we require system 7 we can always use the GetFore/BackColor calls
- // because QD will do the color mapping for us
-
- CRGBColor aColor;
-
- GetBackColor(aColor);
-
- return aColor;
- }
-
- //----------------------------------------------------------------------------------------
- // GetIfColor:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- CRGBColor GetIfColor()
- {
- // Since we require system 7 we can always use the GetFore/BackColor calls
- // because QD will do the color mapping for us
-
- CRGBColor aColor;
-
- GetForeColor(aColor);
-
- return aColor;
- }
-
- //----------------------------------------------------------------------------------------
- // MAGetFontInfo:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- short MAGetFontInfo(FontInfo& theFontInfo)
- // MAGetFontInfo may be used in place of GetFontInfo since it also returns the font height.
- {
- GetFontInfo(&theFontInfo); // get the current port's font info
- return (theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading);// returns the font height
- }
-
- //----------------------------------------------------------------------------------------
- // GetPortFontInfo:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void GetPortFontInfo(short fontNum,
- CStr255& fontName,
- short& fontSize)
- {
- if ((fontNum == systemFont) || (fontNum == GetSysFont()))
- {
- fontName = kSysFontName;
- DefaultSize(fontSize);
- }
- else if ((fontNum == applFont) || (fontNum == GetAppFont()))
- {
- fontName = kApplFontName;
- DefaultSize(fontSize);
- }
- else
- GetFontName(fontNum, fontName);
- }
-
- //----------------------------------------------------------------------------------------
- // GetRandom:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- long GetRandom(long rangeStart,
- long rangeEnd)
- {
- unsigned short rndNum = Random();
- return (rangeStart < rangeEnd) ? rangeStart + rndNum % (rangeEnd - rangeStart + 1) : rangeStart;
- }
-
- //----------------------------------------------------------------------------------------
- // IsColorPort: Returns true if the GrafPort is a Color QD GrafPort test is:
- // IsColorPort = CGrafPtr(port)->portversion == 0xC000
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- Boolean IsColorPort(GrafPtr port)
- {
- return port ? ((((CGrafPtr)port)->portVersion & (short)0xC000)) == (short)0xC000 : FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // IsThisKeyDown:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- Boolean IsThisKeyDown(const short theKey)
- {
- KeyMap map;
-
- GetKeys(map);
- return ((Byte *) & map)[theKey >> 3] & (1 << (theKey & 0x07)) ? TRUE : FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // IsCapsLockKeyDown:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- Boolean IsCapsLockKeyDown()
- {
- static const short kCapsLockKey = 57;
- return IsThisKeyDown(kCapsLockKey);
- }
-
- //----------------------------------------------------------------------------------------
- // IsCommandKeyDown:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- Boolean IsCommandKeyDown()
- {
- static const short kCommandKey = 55;
- return IsThisKeyDown(kCommandKey);
- }
-
- //----------------------------------------------------------------------------------------
- // IsControlKeyDown:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- Boolean IsControlKeyDown()
- {
- static const short kCtlKey = 0x3B;
- return IsThisKeyDown(kCtlKey);
- }
-
- //----------------------------------------------------------------------------------------
- // IsOptionKeyDown:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- Boolean IsOptionKeyDown()
- {
- static const short kOptionKey = 58;
- return IsThisKeyDown(kOptionKey);
- }
-
- //----------------------------------------------------------------------------------------
- // IsShiftKeyDown:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- Boolean IsShiftKeyDown()
- {
- static const short kShiftKey = 56;
- return IsThisKeyDown(kShiftKey);
- }
-
- //----------------------------------------------------------------------------------------
- // LongerSide:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- VHSelect LongerSide(CRect& r)
- {
- return r.GetLength(vSel) >= r.GetLength(hSel) ? vSel : hSel;
- }
-
- //----------------------------------------------------------------------------------------
- // LongerVSide:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- VHSelect LongerVSide(VRect& r)
- {
- return r.GetLength(vSel) >= r.GetLength(hSel) ? vSel : hSel;
- }
-
- //----------------------------------------------------------------------------------------
- // LIntToHex:
- //----------------------------------------------------------------------------------------
- #pragma segment MADebug
-
- void LIntToHex(long decNumber,
- CStr31& hexNumber,
- short noOfDigits)
- {
- short localNoOfDigits = (short)Min(noOfDigits, 8);
- long localDecNumber = decNumber;
- hexNumber[0] = (char)localNoOfDigits;
- for (short i = localNoOfDigits; i >= 1; i--, localDecNumber = localDecNumber >> 4)
- hexNumber[i] = kHexDigits[(localDecNumber & 15)];
- }
-
- //----------------------------------------------------------------------------------------
- // NumberToHex:
- //----------------------------------------------------------------------------------------
- #pragma segment MADebug
-
- void NumberToHex(long theNumber,
- CStr255& hexString,
- short hexDigits)
- {
- CStr31 tempString;
-
- LIntToHex(theNumber, tempString, hexDigits);
- hexString = "$" + tempString;
- }
-
- //----------------------------------------------------------------------------------------
- // PointerToHex:
- //----------------------------------------------------------------------------------------
- #pragma segment MADebug
-
- void PointerToHex(long theNumber,
- CStr31& hexString,
- short hexDigits)
- {
- CStr31 tempString;
-
- if (theNumber == 0)
- hexString = "NULL";
- else
- {
- LIntToHex(theNumber, tempString, hexDigits);
- hexString = "$" + tempString;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // NumBlocks:
- //----------------------------------------------------------------------------------------
- #pragma segment MAFile
-
- long NumBlocks(long numBytes,
- long blkSize)
- {
- return (numBytes + blkSize - 1) / blkSize;
- }
-
-
- static Handle pSaveHText;
- static Handle pMATextBoxHText;
-
- //----------------------------------------------------------------------------------------
- // StdNoRect: StdNoRect filters out the CRect drawing calls.
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- pascal void StdNoRect(GrafVerb /* verb */,
- const CRect& /* r */ )
- {
- }
-
- //----------------------------------------------------------------------------------------
- // InitMyPrivateTE:
- //----------------------------------------------------------------------------------------
- void InitMyPrivateTE(const CRect& box,
- short heapSize)
- {
- static const short kZoneHeaderTrailer = 256;// 256 bytes for header and trailer
- static const short kMPBlockHeader = 32; // 32 bytes for Master Pointer block hdr
- static const short kInitialMstrPtrs = 2; // 2 master pointers created initially
- static const short kSlop = 32; // bytes of slop (just in case)
- static const short kZoneOverhead = kZoneHeaderTrailer + kMPBlockHeader + 4 * kInitialMstrPtrs + kSlop;// how large the zone overhead is
- Ptr aTEZonePtr;
-
- gMATextBoxTE = TENew(box, box);
- if (gMATextBoxTE) // can allocate space for our terecord
- {
- // • save off several items of interest
- gTEDefaultWordBreak = (*gMATextBoxTE)->wordBreak;
- pSaveHText = (*gMATextBoxTE)->hText; // save the text handle
-
- // Since TESetText (called near the end of MATextBox) hits the heap, we can speed this
- // hit to the heap for small text lengths (<= 255), by allocating a special text
- // handle in its own separate heap. We'll use this text handle whenever the text
- // length is <= 255.
-
- // • create a separate heap
- aTEZonePtr = NewPtr(((heapSize + 7) & ~7) + kZoneOverhead);
- if (aTEZonePtr) // can allocate space for our heap
- {
- InitZone(NULL, kInitialMstrPtrs, aTEZonePtr + GetPtrSize(aTEZonePtr), aTEZonePtr);
-
- // • InitZone sets the current zone to the newly created zone
-
- // • allocate our new text handle in our new heap zone
- pMATextBoxHText = NewHandle(heapSize);// the text handle
-
- // • restore the heap zone
- SetZone(ApplicationZone());
- }
- }
- }
-
-
- //----------------------------------------------------------------------------------------
- // MATextBox:
- //----------------------------------------------------------------------------------------
- void MATextBox(Ptr text,
- long itsLength,
- const CRect& box,
- short itsJust,
- Boolean autoWrap,
- ProcPtr wordBreak,
- Boolean eraseFirst,
- Boolean spaceForCaret,
- Boolean preferOutline)
- {
- union QuickDrawProcs
- {
- QDProcs theQDProcs;
- CQDProcs theCQDProcs;
- };
-
-
- static const short kTextBoxCaretSlopSize = 1;// Since TETextBox uses TE to image the
- // text, we may need to adjust by 1 pixel.
- // Reason: TE draws beginning 1 pixel to
- // the right to allow for the insertion
- // point (which we won't have since this
- // is drawn text, not editable text).
- static const short kMaxTEChars = 32000; // Actually TE suffers some other
- // limitations as well. Such as
- // misbehaviour and or bombing when the
- // sum of the lineheights > 32k or a
- // linewidth > 32k (overflows QuickDraw
- // space) But these are _MUCH_ more
- // difficult to test for in a quick way
- static const short kOurHeapSize = 256; // our zone size
-
- QuickDrawProcs myQDProcs;
- QDProcsPtr saveQDProcsPtr;
- WordBreakUPP wbUPP = NULL;
- QDRectUPP saveRectProc;
-
- CWhileOutlinePreferred setOP(preferOutline);
-
- // Create my goodies if necessary
- if (gMATextBoxTE == NULL)
- {
- InitMyPrivateTE(box, kOurHeapSize);
-
- if (gMATextBoxTE == NULL) // couldn't allocate the TE handle
- {
- TETextBox(text, itsLength, box, itsJust);// default to TextBox in low memory
- return;
- }
- }
-
- // Setup the work TE with the necessary parameters
- FontInfo fInfo;
- short lineHeight = MAGetFontInfo(fInfo); // Need to get font's height and ascent.
-
- TERec & workTE = **gMATextBoxTE;
-
- // Intersect the clip and the requested box
- ((CRect &)workTE.viewRect) = ((CRect &)(*TView::GetClipRegion(qd.thePort))->rgnBBox) & box;
- ((CRect &)workTE.destRect) = box;
- if (!spaceForCaret)
- {
- // Widen the destrect but not the visrect. This lets the 1 pixel wide area to the
- // left of all text and the right of all text go unshown.
- workTE.destRect.left -= kTextBoxCaretSlopSize;
- workTE.destRect.right += kTextBoxCaretSlopSize;
- }
-
- workTE.inPort = qd.thePort; // Current port and its characteristics
-
- workTE.txSize = qd.thePort->txSize;
- workTE.txFont = qd.thePort->txFont;
- workTE.txFace = qd.thePort->txFace;
- workTE.fontAscent = fInfo.ascent;
- workTE.lineHeight = lineHeight;
- workTE.just = itsJust;
-
- if (autoWrap)
- workTE.crOnly = 0; //if >=0, word wrap
- else
- workTE.crOnly = -1; //if <0, new line at Return only
-
- workTE.wordBreak = gTEDefaultWordBreak;
-
- if (wordBreak != NULL)
- {
- FailNIL(wbUPP = NewWordBreakProc(wordBreak));
- workTE = **gMATextBoxTE; // re-dereference because memory may have moved
- workTE.wordBreak = wbUPP;
- }
-
- if (pMATextBoxHText != NULL) // if our private heap is set up
- {
- if (itsLength > kOurHeapSize)
- workTE.hText = pSaveHText;
- else
- workTE.hText = pMATextBoxHText;
- }
-
- TESetText(text, Min(itsLength, kMaxTEChars), gMATextBoxTE);
-
- // if called with eraseFirst true, then let TEUpdate image with its built-in EraseRect
- if (eraseFirst)
- TEUpdate(box, gMATextBoxTE);
- else
- {
- /* replace the existing QD procs ( standard or externally supplied )
- so that the EraseRect in TEUpdate is ignored */
- saveQDProcsPtr = qd.thePort->grafProcs;
-
- QDRectUPP theRectProc = NewQDRectProc(&StdNoRect);
- FailNIL(theRectProc);
-
- if (saveQDProcsPtr == NULL)
- {
- if (IsColorPort(qd.thePort))
- {
- SetStdCProcs(&(myQDProcs.theCQDProcs));
- myQDProcs.theCQDProcs.rectProc = theRectProc;
- qd.thePort->grafProcs = (QDProcsPtr) & myQDProcs.theCQDProcs;
- }
- else
- {
- SetStdProcs(&(myQDProcs.theQDProcs));
- myQDProcs.theQDProcs.rectProc = theRectProc;
- qd.thePort->grafProcs = &myQDProcs.theQDProcs;
- }
- }
- else
- {
- saveRectProc = qd.thePort->grafProcs->rectProc;
- qd.thePort->grafProcs->rectProc = theRectProc;
- }
-
- // Now do the imaging
- TEUpdate(box, gMATextBoxTE);
-
- // Restore the QDProcs
- if (saveQDProcsPtr == NULL)
- qd.thePort->grafProcs = NULL;
- else
- qd.thePort->grafProcs->rectProc = saveRectProc;
-
- theRectProc = (QDRectUPP)DisposeIfRoutineDescriptor((UniversalProcPtr)theRectProc);
- }
-
- if (wbUPP)
- {
- DisposeRoutineDescriptor(wbUPP);
- wbUPP = NULL;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // MADrawString:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void MADrawString(const CStr255& s,
- const CRect& box,
- short justification,
- Boolean preferOutline)
- {
- FontInfo theFontInfo;
- short widthOfString;
- short boxWidth;
- CRect localBox = box;
-
- CWhileOutlinePreferred setOP(preferOutline);
-
- GetFontInfo(&theFontInfo);
- widthOfString = StringWidth(s);
- boxWidth = localBox.GetLength(hSel);
- if (widthOfString < boxWidth)
- {
- switch (GetActualJustification(justification))
- {
- case teFlushDefault:
- break;
-
- case teCenter:
- localBox.left += (boxWidth - widthOfString) / 2;
- break;
-
- case teFlushRight:
- localBox.left += boxWidth - widthOfString;
- break;
-
- case teFlushLeft:
- break;
- }
- }
-
- MoveTo(localBox.left, localBox.top + theFontInfo.ascent);
- DrawString(s);
- }
-
- //----------------------------------------------------------------------------------------
- // PinOnRect:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- long PinOnRect(const CRect& theRect,
- CPoint thePt)
- {
- CPoint localPt = thePt;
- if (localPt.h < theRect.left)
- localPt.h = theRect.left;
- if (localPt.h > theRect.right)
- localPt.h = theRect.right;
- if (localPt.v < theRect.top)
- localPt.v = theRect.top;
- if (localPt.v > theRect.bottom)
- localPt.v = theRect.bottom;
-
- return *((long*) & localPt);
- }
-
- //----------------------------------------------------------------------------------------
- // PinOnVRect:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void PinOnVRect(const VRect& theRect,
- const VPoint& thePt,
- VPoint& thePin)
- {
- long hCoord = MinMax(theRect.left, thePt.h, theRect.right);
- long vCoord = MinMax(theRect.top, thePt.v, theRect.bottom);
- thePin = VPoint(hCoord, vCoord);
- }
-
- //----------------------------------------------------------------------------------------
- // RoundUp:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- long RoundUp(long aNumber,
- short aModulus)
- {
- return ((aNumber + aModulus - 1) / aModulus) * aModulus;
- }
-
- //----------------------------------------------------------------------------------------
- // SetKeyScript:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- short SetKeyScript(short newKeyScript)
- {
- short currentKeyScript = (short)GetScriptManagerVariable(smKeyScript);
- if (currentKeyScript != newKeyScript) // Don't unnecessarily annoy the script mgr
- KeyScript(newKeyScript);
- return currentKeyScript;
- }
-
- //----------------------------------------------------------------------------------------
- // SetIfBkColor:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void SetIfBkColor(const CRGBColor& aColor)
- {
- // Since we require system 7 we can always use the RGBFore/BackColor calls
- // because QD will do the color mapping for us
-
- // Try to avoid calling RGBFore/BackColor unnecessarally
- if (!IsColorPort(qd.thePort) || (((CRGBColor &) ((CGrafPtr) qd.thePort)->rgbBkColor) != aColor))
- RGBBackColor(aColor);
- }
-
- //----------------------------------------------------------------------------------------
- // SetIfColor:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void SetIfColor(const CRGBColor& aColor)
- {
- // Since we require system 7 we can always use the RGBFore/BackColor calls
- // because QD will do the color mapping for us
-
- // Try to avoid calling RGBFore/BackColor unnecessarally
- if (!IsColorPort(qd.thePort) || (((CRGBColor &) ((CGrafPtr) qd.thePort)->rgbFgColor) != aColor))
- RGBForeColor(aColor);
- }
-
- //----------------------------------------------------------------------------------------
- // GetPortTextStyle:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void GetPortTextStyle(TextStyle& theTextStyle)
- {
- theTextStyle.tsFont = qd.thePort->txFont;
- theTextStyle.tsFace = qd.thePort->txFace;
- theTextStyle.tsSize = qd.thePort->txSize;
- theTextStyle.tsColor = GetIfColor();
- }
-
- //----------------------------------------------------------------------------------------
- // MAGetTextStyle:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void MAGetTextStyle(ResNumber rsrcID,
- TextStyle& theTextStyle)
- {
- MATextStyleHandle theTextStyleHdl = (MATextStyleHandle)GetResource('TxSt', rsrcID);
- FailNILResource((Handle)theTextStyleHdl);
-
- MATextStyle & resourceStyle = **theTextStyleHdl;
-
- theTextStyle.tsFace = resourceStyle.tsFace;
- theTextStyle.tsSize = resourceStyle.tsSize;
- theTextStyle.tsColor = *((RGBColor *) & (resourceStyle.tsColor));
-
- CStr255 itsFont((*theTextStyleHdl)->tsFont);
- theTextStyle.tsFont = GetFontNum(itsFont);
- }
-
- //----------------------------------------------------------------------------------------
- // SetPortTextStyle:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void SetPortTextStyle(const TextStyle& theTextStyle)
- {
- if (qd.thePort->txFont != theTextStyle.tsFont)
- TextFont(theTextStyle.tsFont);
- if (qd.thePort->txFace != theTextStyle.tsFace)
- TextFace(theTextStyle.tsFace);
- if (qd.thePort->txSize != theTextStyle.tsSize)
- TextSize(theTextStyle.tsSize);
- SetIfColor(theTextStyle.tsColor);
- }
-
- //----------------------------------------------------------------------------------------
- // SetTextStyle:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void SetTextStyle(TextStyle& theTextStyle,
- short theFont,
- /* Style */
- short theStyle,
- short theSize,
- const CRGBColor& theColor)
- {
- theTextStyle.tsFont = theFont;
- theTextStyle.tsFace = theStyle;
- theTextStyle.tsSize = theSize;
- theTextStyle.tsColor = theColor;
- }
-
- //----------------------------------------------------------------------------------------
- // UseSelectionColor:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void UseSelectionColor()
- {
- unsigned char hiliteModeValue = LMGetHiliteMode();
- BitClr((Ptr) & hiliteModeValue, pHiliteBit);
- LMSetHiliteMode(hiliteModeValue);
- }
-
- //----------------------------------------------------------------------------------------
- // CheckAdornment:
- //----------------------------------------------------------------------------------------
- #pragma segment MADebug2
-
- void CheckAdornment(CntlAdornment p,
- CntlAdornment alias,
- CStr255 theString,
- char* name)
- {
- // "set1 <= set2" means set1 is wholly contained in set2
- if (p <= alias)
- if (theString == CStr255("["))
- theString = theString + name;
- else
- theString = theString + CStr255(",") + name;
- }
-
-
- //----------------------------------------------------------------------------------------
- // CheckStyleItem:
- //----------------------------------------------------------------------------------------
- void CheckStyleItem(unsigned char s,
- unsigned char alias,
- CStr255 theString,
- char* name)
- {
- if (s & alias)
- if (theString == CStr255("["))
- theString = theString + name;
- else
- theString = theString + CStr255(",") + name;
- }
-
- //----------------------------------------------------------------------------------------
- // RotatePat:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
- void RotatePat(Pattern& aPattern)
- {
- unsigned char temp = aPattern.pat[0];
- for (short index = 0; index < (sizeof(Pattern) - 1); index++)
- aPattern.pat[index] = aPattern.pat[index + 1];
- aPattern.pat[sizeof(Pattern) - 1] = temp;
- }
-
- //----------------------------------------------------------------------------------------
- // XorPat:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
- void XorPat(const Pattern& patA,
- const Pattern& patB,
- Pattern& toPat)
- {
- for (short index = 0; index < sizeof(Pattern); index++)
- toPat.pat[index] = patA.pat[index] ^ patB.pat[index];// xor
- }
-
- //----------------------------------------------------------------------------------------
- // StreamToHandle:
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- Handle StreamToHandle(TStream* theStream)
- {
- MAVolatileInit(Handle, dataHandle, NULL);
- Size dataSize = theStream->GetSize() - theStream->GetPosition();
-
- FailInfo fi;
- Try(fi)
- {
- dataHandle = NewPermHandle(dataSize); // fails if error
-
- HLock(dataHandle);
- theStream->ReadBytes(*dataHandle, dataSize);
- HUnlock(dataHandle);
- fi.Success();
- }
- else // Recover
- {
- dataHandle = DisposeIfHandle(dataHandle);
- }
-
- return dataHandle;
- }
-
- //----------------------------------------------------------------------------------------
- // End of UMacAppUtilities.cp
-
- #pragma segment Inline
-
-